package com.hy.chat.client.view;



import java.util.ArrayList;
import java.util.Date;
import java.util.List;


import com.extjs.gxt.ui.client.GXT;
import com.extjs.gxt.ui.client.Registry;
import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.Style.Scroll;
import com.extjs.gxt.ui.client.data.BaseModel;
import com.extjs.gxt.ui.client.data.DataProxy;
import com.extjs.gxt.ui.client.data.Loader;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.util.Theme;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.Html;
import com.extjs.gxt.ui.client.widget.Info;
import com.extjs.gxt.ui.client.widget.Layout;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.TabItem;
import com.extjs.gxt.ui.client.widget.TabPanel;
import com.extjs.gxt.ui.client.widget.Window;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.HtmlEditor;
import com.extjs.gxt.ui.client.widget.layout.FitData;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dev.jjs.impl.GwtAstBuilder;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.i18n.client.DateTimeFormat.PredefinedFormat;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.hy.chat.client.Chat;
import com.hy.chat.client.ChatServiceAsync;
import com.hy.chat.client.presenter.AdminChatPersenter;
import com.hy.chat.client.presenter.AnonymousChatPersenter.Display;
import com.hy.chat.client.view.AdminChatView;
import com.hy.chat.client.widget.ChatTabItem;
import com.hy.chat.client.widget.ChatWindow;



public class AnonymousChatView extends DecoratorPanel implements Display{
	
	
	

	public ChatTabItem getChatTabItem() {
		return chatTabItem;
	}

	public Button getSentButton() {
		return sentButton;
	}

	public ChatWindow getWindow() {
		return window;
	}

	public TabPanel getTabPanel() {
		return tabPanel;
	}

	public HtmlEditor getHtmlEditor() {
		return htmlEditor;
	}

	final Button sentButton = new Button("发送");
	ChatWindow window = new ChatWindow("在线咨询");
	final TabPanel tabPanel = new TabPanel();  
    final ChatTabItem chatTabItem = new ChatTabItem("与在线客服会话<br>");  

	final HtmlEditor htmlEditor = new HtmlEditor();  

	/*private final ChatServiceAsync greetingService = GWT
			.create(ChatService.class);*/
	private ChatServiceAsync greetingService= Registry.get(Chat.CHAT_SERVICE);

	private static final String  ICON = "myGwt-image1";
	
	public AnonymousChatView() {
		super();			
				
				ContentPanel contentPanel = new ContentPanel();
				contentPanel.setHeaderVisible(false);
				
				   // folder.setWidth(450);  
				    tabPanel.setHeight(250);  
				  
				    chatTabItem.addStyleName("pad-text");  
				    chatTabItem.setHeight(150);
				    chatTabItem.setIconStyle(ICON);
				    //chatTabItem.setAutoHeight(true);
				    chatTabItem.setScrollMode(Scroll.AUTOY); 
				    chatTabItem.setData("messageIndex", 0);
				    Html html = chatTabItem.addText("您可以与客服人员进行在线咨询与洽谈");  
				    //final ChatTabItem chatTabItem = this.createChatTabItem("与在线客服会话<br>", "您可以与客服人员进行在线咨询与洽谈 ");
		
				    tabPanel.add(chatTabItem);  
				    //folder.setTabScroll(true);  
				    
				    		//folder.set
					/*for (int i = 0; i < 8; i++) {
						TabItem item = new TabItem(i+"ahhhhhhhhhhhhdfa");
						item.setClosable(true);
					    folder.add(item);  
		
					}*/
				    //folder.setCloseContextMenu(true);  
		
				   // folder.setc
				contentPanel.add(tabPanel);
		//		ContentPanel editPanel = new ContentPanel();
		//		editPanel.setHeaderVisible(false);
				this.configureHtmlEditor(htmlEditor);
				
		//	    contentPanel.add(htmlEditor, new FormData("100% -53"));  
		//		editPanel.add(htmlEditor, new FormData("100% -53"));  
			    		    	
			    	//editPanel .addButton(button);
			    	//editPanel.setButtonAlign(HorizontalAlignment.RIGHT);
				    contentPanel.add(htmlEditor, new FormData("100% -53"));  
			   	  contentPanel.addButton(sentButton);
			   	contentPanel.setButtonAlign(HorizontalAlignment.RIGHT);
		
		
		//	window.add(folder);
			window.add(contentPanel);
		//	System.out.println("000 :"+RootPanel.get("000"));
			
			//window.el().mask("请稍等。。。。") ;
			
			
			this.add(window);
	}
	
	
	
	private ChatTabItem createChatTabItem (String sessionId,String text) {
		ChatTabItem chatTabItem = new ChatTabItem(sessionId);  
		    chatTabItem.addStyleName("pad-text");  
		    chatTabItem.setHeight(100);  
		    chatTabItem.setData("messageIndex", 0);
		    chatTabItem.setScrollMode(Scroll.AUTOY); 

		    Html html = chatTabItem.addText(text);  
		    
		    return chatTabItem;
	}
	
	private ChatTabItem createChatTabItem (String sessionId,String message,int index) {
		
		    return this.createChatTabItem(sessionId, message, index, false);
	}
	
	private ChatTabItem createChatTabItem (String sessionId,String message,int index,boolean closable) {
		ChatTabItem chatTabItem = new ChatTabItem("与客户"+sessionId.substring(0, 6)+"会话");  
		    chatTabItem.addStyleName("pad-text");  
		    chatTabItem.setHeight(100);  
		    chatTabItem.setIconStyle(ICON);

		    chatTabItem.setData("messageIndex", index);
		    chatTabItem.setData("sessionId", sessionId);
		    chatTabItem.setClosable(closable);
//		    chatTabItem.addText(message);  
		    chatTabItem.setScrollMode(Scroll.AUTOY); 

		    Html xx =chatTabItem.addText(message);  
		   // chatTabItem.focus();
		    return chatTabItem;
	}
	
	private String getCurrentDateTime() {
		return DateTimeFormat.getFormat(PredefinedFormat.DATE_TIME_SHORT).format(new Date());
	}
	
	private HtmlEditor configureHtmlEditor(HtmlEditor htmlEditor) {
		htmlEditor.setHeight( 100);
		htmlEditor.setWidth(550);
		htmlEditor.setHideLabel(true);  
		return htmlEditor;
	}
	
	
	
	
	
}
